﻿using AiXiu.Common;
using AiXiu.DAL;
using AiXiu.IBLL;
using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.BLL
{
    public class VideoManager : IVideoManager
    {
        public List<TBvideos> GetVideosByUserId(int userId)
        {
            IVodService vodService = new VodService();
            return vodService.GetVideosByUserId(userId);
        }

        public OperResult<CreateUploadVideoResult> ToBeUpload(int userId, string fileName, string heading, string location = null)
        {
            //获取凭证
            VodHelper vodHelper = new VodHelper();
            CreateUploadVideoResult createUpload = vodHelper.CreateUploadVideo(heading, fileName);
            //写入数据库
            IVodService vodService = new VodService();
            vodService.ToBeUpload(createUpload.VideoId, userId, heading, location);
            return OperResult<CreateUploadVideoResult>.Succeed(createUpload);
        }
        public int UpdateVideos()
        {
            IVodService vodService = new VodService();
            List<string> videoIds = vodService.GetInProcessVideoIds();
            if (videoIds.Count > 0)
            {
                VodHelper vodHelper = new VodHelper();
                GetVideoInfosResult result = vodHelper.GetVideoInfos(videoIds);
                List<TBvideos> bvideos = result.VideoList.Select(p=> new TBvideos() {
                    VideoId=p.VideoId,
                    Headline=p.Title,
                    CoverURL=p.CoverURL,
                    Status=((int)p.Status).ToString()
                }).ToList();
                vodService.UpdateVideos(bvideos);
                return videoIds.Count;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 做数据库和阿里云删除
        /// </summary>
        /// <param name="videoId"></param>
        public void DeleteVideo(string videoId)
        {
            //从数据库删除
            IVodService videoService = new VodService();
            videoService.DeleteVideo(videoId);

            //从阿里云删除
            VodHelper vodHelper = new VodHelper();
            List<string> videoIds = new List<string>();
            videoIds.Add(videoId);
            vodHelper.DeleteVideos(videoIds);
        }

        public List<TBvideos> GetVidos()
        {
            IVodService vodService = new VodService();
            return vodService.GetVidos();
        }
        public OperResult<TBvideos>GetVideoByVideoId(string VideoId)
        {
            IVodService vodService = new VodService();
            TBvideos vidoeo = vodService.GetVideoByVideoId(VideoId);
            return OperResult<TBvideos>.Succeed(vidoeo);
        }
        public OperResult<GetPlayInfoResult> InitPlay(string Video)
        {
            VodHelper vodHelper = new VodHelper();
            GetPlayInfoResult getPlayInfo = vodHelper.GetPlayInfo(Video);
            return OperResult<GetPlayInfoResult>.Succeed(getPlayInfo);
        }
    }
}
